package org.example.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.bean.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentTest {
    InputStream is = null;
    SqlSessionFactory sqlSessionFactory = null;
    SqlSession sqlSession = null;


    @Before
    public void beforemethod()throws IOException{
        is = Resources.getResourceAsStream("MyBatisConfig.xml");//加载
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//获取SqlSessionFactoty
        sqlSession = sqlSessionFactory.openSession(true);//通过2获取sqlSession
                                                      //自动提交
    }


    @After
    public void end() throws IOException{
        sqlSession.close();//释放资源
        is.close();
    }


    @Test
    public void delete(){
        int delete = sqlSession.delete("StudentMapper.delete",4);//删
    }

    @Test
    public void update(){
        Student student = new Student(4,"hh",20);//改
        sqlSession.update("StudentMapper.update",student);
    }
    @Test
    public void insert(){
        Student student = new Student(4,"zz",18);//增
        sqlSession.insert("StudentMapper.insert",student);
        //sqlSession.commit();
    }


    @Test
    public void selectById() throws IOException {
       Student stu = sqlSession.selectOne("StudentMapper.selectById",2);//查找
       System.out.println("stu =" + stu);
    }


    @Test
    public void selectAll() throws IOException {
        List<Object> List = sqlSession.selectList("StudentMapper.selectAll");//全部
        List.forEach(System.out :: println);

    }
}
